﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flyline : MonoBehaviour
{
    // Use this for initialization
    Vector3 pos;
    Vector3 pos1;
    Vector3 pos2;
    Vector3 scale0;
    float objectHeight = 2.0f;


    void Start()
    {
        scale0 = transform.localScale; 
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Init.isFly == 0)
        {
            //flyline.transform.position = new Vector3(0, 0, 0);
            //flyline.gameObject.SetActive(false);
        }
        else
        {
        if (Init.isFly==1)
        {
         //pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
         // pos1 = Camera.main.ScreenToWorldPoint(pos1);  
            pos1 = Init.startFly;
            pos2 = pos1;
            Init.isFly = 2;

        }

        else if (Init.isFly==2)
        {
            pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
            pos2 = Camera.main.ScreenToWorldPoint(pos2);
        }

        //Debug.Log("pos1:" + pos1);
        //Debug.Log("pos2:" + pos2);
        if (pos2 != pos1)
        {
            Vector3 v3 = pos2 - pos1;
            transform.position = pos1 + (v3) / 2.0f;
            //transform.localScale.y= v3.magnitude / objectHeight;
            transform.localScale = new Vector3(0.002f, v3.magnitude / objectHeight, 0.002f);
            transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
        }

            if (Input.GetMouseButtonDown(1))
            {
                pos1 = new Vector3(0f,0f,0f);
                pos2 = pos1;
                transform.localScale = scale0;
                transform.gameObject.SetActive(false);
                //Init.isFly = 0;
                Init.startPoint = "";
                Init.endPoint = "";

            }
       }

    }
}

